import sqlite3
from tqsdk import TqApi, TqAuth, TqKq
import os

# 获取当前脚本文件的绝对路径所在目录
# 注意：__file__ 是相对路径，需要转换为绝对路径
dir=os.path.abspath(__file__)
print(dir)
current_dir = os.path.dirname(os.path.abspath(__file__))
print(current_dir)
# 构建数据库的绝对路径，../ 表示上一级目录，这里需要上两级
db_path = os.path.join(current_dir, '../../trade_monitor.db')

# 连接到数据库
try:
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    # 查询待监控的品种
    cursor.execute("SELECT * FROM products WHERE is_monitored=1")
    products = cursor.fetchall()
    symbols = [row[2] for row in products]
    print(symbols)
    conn.close()
except Exception as e:
    print(f"从数据库获取监控品种时出错: {e}")
    symbols = []

# 初始化 TqApi
api = TqApi(account=TqKq(), auth=TqAuth("cps168", "alibaba"))

try:
    for symbol in symbols:
        quote = api.get_quote(symbol)
        # 等待行情数据更新
        #api.wait_update()
        volume_multiple = quote.volume_multiple
        price_tick = quote.price_tick
        print(f"品种: {symbol}, 合约乘数(volume_multiple): {volume_multiple}, 最小变动价位(price_tick): {price_tick}")
except Exception as e:
    print(f"获取行情信息时出错: {e}")
finally:
    api.close()